package root.electronclass.electronclass.common.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorInt;


import root.electronclass.electronclass.R;

import java.util.Timer;
import java.util.TimerTask;


public class K12DialogHelper {

    TextView mContentTv;
    ImageView mIcon;

    private Context mContext;
    private Dialog k12Dialog;
    private String mContent;
    private String mIconContent;
    private int mContentColor = 0;
    private int mIconColor = 0;

    public void setDialogDismissLisentner(DialogInterface.OnDismissListener dialogDismissLisentner) {
        k12Dialog.setOnDismissListener(dialogDismissLisentner);
    }

    public K12DialogHelper(Context mContext) {
        this.mContext = mContext;
        k12Dialog = new Dialog(mContext, R.style.K12DialogStyle);
        k12Dialog.setContentView(R.layout.dialog_k12);
        mContentTv = k12Dialog.findViewById(R.id.dialog_content);
        mIcon = k12Dialog.findViewById(R.id.dialog_icon);
    }

    public void setIconContent(String iconContent) {
        mIconContent = iconContent;
    }

    public void setTextContent(String mContent) {
        this.mContent = mContent;
    }

    public void setContentColor(int contentColor) {
        mContentColor = contentColor;
    }

    public void setIconColor(int iconColor) {
        mIconColor = iconColor;
    }

    public void apply() {
        if (!TextUtils.isEmpty(mContent)) {
            setContent(mContent);
        }
        if (!TextUtils.isEmpty(mIconContent)) {
            setIcon(mIconContent);
        }
        if (mContentColor != 0) {
            mContentTv.setTextColor(mContentColor);
        }
        if (mIconColor != 0) {

        }
    }

    public void setContent(String res) {
        mContentTv.setText(res);
    }

    public String getContext() {
        return mContentTv.getText().toString();
    }

    public void setIcon(String res) {
    }

    public void show() {
        if (!((Activity) mContext).isFinishing()) {

            k12Dialog.show();
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    if (k12Dialog != null && k12Dialog.isShowing()) {
                        k12Dialog.dismiss();
                    }
                }
            }, 2000);
        }
    }

    public boolean isShow() {
        return k12Dialog.isShowing();
    }

    public void dismiss() {
        if (k12Dialog != null && isShow()) {
            k12Dialog.dismiss();
        }
    }

    public static class Builder {
        private K12DialogHelper mK12DialogHelper;

        public Builder(Context mContext) {
            mK12DialogHelper = new K12DialogHelper(mContext);
        }

        public Builder setContent(String content) {
            mK12DialogHelper.setTextContent(content);
            return this;
        }

        public Builder setIconContent(String iconContent) {
            mK12DialogHelper.setIconContent(iconContent);

            return this;
        }

        public Builder setContentColor(@ColorInt int contentColor) {
            mK12DialogHelper.setContentColor(contentColor);
            return this;
        }

        public Builder setIconColor(@ColorInt int iconColor) {
            mK12DialogHelper.setIconColor(iconColor);
            return this;
        }

        public K12DialogHelper builder() {
            mK12DialogHelper.apply();
            return mK12DialogHelper;
        }
    }
}
